﻿

#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UFIDA.U9.Base.HoldRelease;
using UFIDA.U9.CUST.AITBarCodeBP;
using UFSoft.UBF.Business;

#endregion

namespace UFIDA.U9.CUST.AITBarCodeBE {

	public partial class AITBarCode{

		//private Dictionary<string, string> _changeDic=new Dictionary<string, string>()
		//{
  //          { "ID", "ID" },
  //          { "SysVersion", "SysVersion" },
  //          { "CreatedOn", "CreatedOn" },
  //          { "CreatedBy", "CreatedBy" },
  //          { "ModifiedOn", "ModifiedOn" },
  //          { "ModifiedBy", "ModifiedBy" },
  //          { "BarCode", "BarCode" },
  //      };
		//private bool changeBarCode=false;

		#region Custom Constructor

		#endregion

		#region before & after CUD V 
		/*	实体提交的事件顺序示例(QHELP) 主实体A 组合 非主实体B .
		/ (新增A和B两个实例)A.OnSetDefaultValue->B.OnSetDefaultValue-> B.OnValidate ->A.OnValidate ->A.OnInserting ->B.OnInserting ->产生提交SQL ->B.OnInserted ->A.OnInserted
		/ (仅修改B,A也会被修改))A.OnSetDefaultValue->B.OnSetDefaultValue-> B.OnValidate ->A.OnValidate ->A.OnUpdating ->B.OnUpdating ->产生提交SQL ->B.OnUpdated ->A.OnUpdated
		/ (删除A,B会被级联删除))A.OnDeleting ->B.OnDeleting ->产生提交SQL ->B.OnDeleted ->A.OnDeleted
		/	产生提交SQL顺序则看其外键，增修一对多先A后B，一对一先B后A。　删除一对多先B后A，一对一先A后B .
		*/	
		/// <summary>
		/// 设置默认值
		/// </summary>
		protected override void OnSetDefaultValue()
		{
			base.OnSetDefaultValue();
			
		}
		/// <summary>
		/// before Insert
		/// </summary>
		protected override void OnInserting() {
			base.OnInserting();
			// TO DO: write your business code here...
		}

		/// <summary>
		/// after Insert
		/// </summary>
		protected override void OnInserted() {
			base.OnInserted();
			// TO DO: write your business code here...
			// 条码更新提供单独按钮,这里不再更新
            //this.UpdateBarCode();
        }

		private void UpdateBarCode()
		{
			if (string.IsNullOrEmpty(this.BarCode))
			{
				using (ISession session = Session.Open())
				{
					GetBarCode bp = new GetBarCode();
					bp.ID = this.ID;
					string barcode = bp.Do();
					this.BarCode = barcode;
					session.Modify(this);
					session.Commit();
				}
			}
		}

		/// <summary>
		/// before Update
		/// </summary>
		protected override void OnUpdating() {
			base.OnUpdating();
			// TO DO: write your business code here...
		}

		/// <summary>
		/// after Update
		/// </summary>
		protected override void OnUpdated() {
			base.OnUpdated();
			// TO DO: write your business code here...
			//if (this.IsDataChanged())
			//{
			//	//数据变化
			//	if (this.PrintNum == 0)
			//	{
			//		//尚未打印
			//		//重新更新条码值
   //                 this.UpdateBarCode();
   //             }
			//}
        }

		/// <summary>
		/// 判断属性变化
		/// 排出系统字段
		/// </summary>
		/// <returns></returns>
   //     private bool IsDataChanged()
   //     {
			//if (this.OriginalData.ID > 0)
			//{
			//	//实体修改场景
   //             List<string> list = this.GetChangedAttributes();
   //             if (list != null && list.Count > 0)
   //             {
   //                 foreach (var item in list)
   //                 {
   //                     if (!_changeDic.ContainsKey(item))
   //                         return true;
   //                 }
   //             }
   //         }
			//return false;
   //     }


        /// <summary>
        /// before Delete
        /// </summary>
        protected override void OnDeleting() {
			base.OnDeleting();
			// TO DO: write your business code here...
			if (this.PrintNum > 0) 
				throw new Exception($"条码[{BarCode}]已经打印,不能删除");
		}

		/// <summary>
		/// after Delete
		/// </summary>
		protected override void OnDeleted() {
			base.OnDeleted();
			// TO DO: write your business code here...
		}

		/// <summary>
		/// on Validate
		/// </summary>
		protected override void OnValidate() {
			base.OnValidate();
			this.SelfEntityValidator();
			// TO DO: write your business code here...
		}
		#endregion
		
		#region 异常处理，开发人员可以重新封装异常
		public override void  DealException(Exception e)
        	{
          		base.DealException(e);
          		throw e;
        	}
		#endregion

		#region  扩展属性代码区
		
		#endregion

		#region CreateDefault
		private static AITBarCode CreateDefault_Extend() {
			//TODO delete next code and add your custome code here
			throw new NotImplementedException () ;
		}
		/// <summary>
		/// Create DefaultComponent
		/// </summary>
		/// <returns>DefaultComponent Instance</returns>
		private  static AITBarCode CreateDefaultComponent_Extend(){
			//TODO delete next code and add your custome code here
			throw new NotImplementedException () ;
		}
		
		#endregion 






		#region Model Methods
		#endregion		
	}
}
